package system

import (
	"sensitives/service/dic"
	"strings"
)

type commonfilter struct {
	Sensitiveword []sensitive
}
type sensitive struct {
	Word  []string `json:"word"`
	Level uint8    `json:"level"`
	Type  string   `json:"type"`
}

func (cf *commonfilter) filter(text, types string, sys_dic *dic.TrieTree) {
	texts := strings.Split(text, "")
	texts_length := len(texts)
	for i := 0; i < texts_length; i++ {
		tmpText := texts[i]
		TempMap := sys_dic
		if _, ok := TempMap.Node[tmpText]; !ok {
			continue
		}
		TempMap = TempMap.Node[tmpText]
		if TempMap.IsEnd {
			sensi := sensitive{
				Word:  []string{"", tmpText, ""},
				Type:  types,
				Level: TempMap.Level,
			}
			cf.Sensitiveword = append(cf.Sensitiveword, sensi)
		}
		for j := i + 1; j < texts_length; j++ {
			jtext := texts[j]
			Tmpj, ok := TempMap.Node[jtext]
			if !ok {
				break
			} else if ok && Tmpj.IsEnd {
				tmpText += jtext
				sensi := sensitive{
					Word:  []string{"", tmpText, ""},
					Type:  types,
					Level: Tmpj.Level,
				}
				cf.Sensitiveword = append(cf.Sensitiveword, sensi)
				TempMap = Tmpj
			}
		}
	}
}
